<?php
session_start();
include_once( 'config.php' );
include_once( 'weibooauth.php' );
$nameArr = array('东经109度23分琬','诚心感恩感','宇宙无敌龅牙葵福');
/*$c_c=count($nameArr)-1;
$i=rand(0,$c_c);
$userName = $nameArr[$i];*/
$c_c = shunxu(count($nameArr));
$userName = $nameArr[$c_c];
$sql1="select * from cai_uid where sns_id='".SNS_ID."' and name='".trim($userName)."'"; 
$result1 = mysql_query($sql1);
while($row1=mysql_fetch_array($result1)){
    $token = explode('_',$row1['token']);
    $token1 = $token[0];
    $token2 = $token[1];
}
define('TOKEN1',$token1);
define('TOKEN2',$token2);
$c = new WeiboClient(WB_AKEY ,WB_SKEY,TOKEN1,TOKEN2);

$contentArr = array('您好，喜欢宠物的盆友欢迎关注@萌乐宠官网 喵~。', '喵~~喜欢宠物的朋友欢迎关注@萌乐宠官网 嘻嘻', '喜欢宠物么？@萌乐宠官网 各种宠物，各种卖萌~~喵~', '喵一个，@萌乐宠官网 各种宠物萌图喵星人汪星人，欢迎关注。', '您如果喜欢宠物欢迎关注 @萌乐宠官网 各种好玩的宠物图片，非常萌哦~~~');
$c_c=count($contentArr)-1;
$i=rand(0,$c_c);
$content = $contentArr[$i];
$cate = 'chongwu';
$userid=suiji($cate);

$weibo = $c->show_user($userid);
while(isset($weibo['statuses_count']) && $weibo['statuses_count'] == 0){
    $userid=suiji($cate);
    $weibo = $c->show_user($userid);
}

/*发表评论*/
$result=$c->send_comment( $weibo['status']['mid'] ,$content);
//dump($result);

if(isset($result['error']) && $result['error_code'] > 0){
    echo $result['user']['name'].'<br/>发布失败<br/>';
    $text = "错误：".$result['error_code']."+".$result['error']."<br/>发送账号：".$userName."<br/>内容：".$content;
    writelog($text,1);
}else{
    echo $result['user']['name'].'发布成功<br/>';
    $text="微博用户：http://weibo.com/".$result['status']['user']['id']."<br/>发送账号：".$result['user']['name'];
    writelog($text,0);
}
function writelog($str,$success)
{
    include_once( 'config.php' );
    $con=mysql_connect(DB_HOST,DB_USER,DB_PASSWORD); 
    mysql_select_db(DB_NAME,$con);
    mysql_query("SET NAMES utf8");
    $sql="INSERT INTO `cai_log` (`id`, `success`, `content` , `data`,`from`,`sns`) VALUES (NULL,'".$success."','".$str."','".time()."','huifu','weibo');";
    $result=mysql_query($sql);
    if($result){
        echo '记录数据成功<br/>';
    }else{
        echo '记录数据失败<br/>';
    }
}

function suiji($cate){
    include_once( 'config.php' );
    $con=mysql_connect(DB_HOST,DB_USER,DB_PASSWORD); 
    mysql_select_db(DB_NAME,$con);
    mysql_query("SET NAMES utf8");
    //$sql="select * from cai_fensi where done='0' and cate='".$cate."' and sns='weibo' ORDER BY rand() LIMIT 1"; 
    $sql="select * from cai_fensi where done='0' and cate='".$cate."' and sns='weibo' LIMIT 1"; 
    $result=mysql_query($sql);
    $row = mysql_fetch_array($result, MYSQL_ASSOC);
    $id=$row['id'];
    $user_id=$row['user_id'];
    $sql2="UPDATE `cai_fensi` SET `done` ='1' WHERE  id='".$id."' and sns='weibo'";
    $result2=mysql_query($sql2);
    if($result2){
        return $user_id;
    }else{
        return '0';
    }  
}
function shunxu($max_num){
    include_once( 'config.php' );
    $con=mysql_connect(DB_HOST,DB_USER,DB_PASSWORD); 
    mysql_select_db(DB_NAME,$con);
    mysql_query("SET NAMES utf8");
    $sql="select * from cai_shunxu where id =1"; 
    $result=mysql_query($sql);
    $row = mysql_fetch_array($result, MYSQL_ASSOC);
    $num_data=$row['num']+1;
    if($max_num<=$num_data){
        $num_data = 0;
    }
    $sql2="UPDATE `cai_shunxu` SET `num` ='".$num_data."' WHERE  id='1'";
    $result2=mysql_query($sql2);
    if($result2){
        return $num_data;
    }else{
        return 'error';
    }  
}

/*获取用户的粉丝*/
/*$funs=$c->followers(1,10,'1517824775');
dump($funs);*/

/*获取用户的最新微博*/
/*$new_status=$c->show_user( '1517824775');
dump($new_status);*/

/*发表评论*/
/*$content = '微博内容';
$result=$c->send_comment( $new_status['status']['mid'] ,$content);
dump($result);*/
?>




